package com.hhh.ExceptionHandle;

import com.hhh.Pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.sql.SQLException;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //    处理SQL异常
    @ExceptionHandler(SQLException.class)
    public Result SqlExceptionHandle(SQLException s) {
        String Excetionmsg = s.getMessage();
        log.info(Excetionmsg);
        if (Excetionmsg.contains("Duplicate entry")) {
            return Result.error("用户已存在");
        }
        return Result.error("SQL错误，请联系管理员");
    }

    //    处理IO异常
    @ExceptionHandler(IOException.class)
    public Result ExceptionHandle(IOException i) {
        String Excetionmsg = i.getMessage();
        log.info(Excetionmsg);
        return Result.error("io问题，请联系管理员");
    }

    //    处理分类异常
    @ExceptionHandler(CategoryException.class)
    public Result ExceptionHandle(CategoryException c) {
        return Result.error("关联了套餐或者菜品,无法删除分类");
    }

    //    处理菜品异常
    @ExceptionHandler(DishException.class)
    public Result ExceptionHandle(DishException d) {
        return Result.error("菜品未停售,无法删除");
    }

    //    处理套餐异常
    @ExceptionHandler(SetmealException.class)
    public Result ExceptionHandle(SetmealException s) {
        return Result.error("套餐未停售,先停售");
    }

    @ExceptionHandler(UserException.class)
    public Result ExceptionHandle(UserException u) {
        return Result.error("未登录用户，请登录");
    }

    @ExceptionHandler(AddressBookException.class)
    public Result ExceptionHandle(AddressBookException a) {
        return Result.error("地址不正确");
    }

}
